package com.santi.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.santi.entity.Book;
import com.santi.entity.ResultMessage;
import com.santi.mapper.BookMapper;
import com.santi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService service;
    @RequestMapping("/list")
    public ResultMessage list(@RequestParam(defaultValue = "1")Integer pageNo,
                              @RequestParam(defaultValue = "5")Integer pageSize){

        List<Book> list = null;
        Page<Book> page = PageHelper.startPage(pageNo, pageSize);
        ResultMessage message = new ResultMessage();
        try {
           list= service.queryAll();
           message.setCode(200);
           message.setMsg("成功");
            int totalPage  = page.getPages();
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("totalPage", totalPage);
            data.put("list", list);
          message.setData(data);
        } catch (Exception e) {
            message.setCode(301);
            message.setMsg("错误");
        }
        return message;


    }
}
